package cn.zl.common.auth;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 监听 Shiro 会话的生命周期事件
 * @author zl
 * @date 2023/9/12
 */
public class ShiroSessionListener implements SessionListener {

    //跟踪当前活动的会话数量。初始值为0
    private final AtomicInteger sessionCount = new AtomicInteger(0);

    //新会话开始时,会话数量会自增
    @Override
    public void onStart(Session session) {
        sessionCount.incrementAndGet();
    }

    //当会话停止时,数量自减
    @Override
    public void onStop(Session session) {
        sessionCount.decrementAndGet();
    }

    //会话过期时,数量自减
    @Override
    public void onExpiration(Session session) {
        sessionCount.decrementAndGet();
    }
}
